package com.aree.caipiao.shsq.clawer;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.aree.caipiao.shsq.dao.IShuangSeQiuResultDao;
import com.aree.caipiao.shsq.dao.jdbc.ShuangSeQiuResultDao;
import com.aree.caipiao.shsq.model.ShuangSeQiuResult;
import com.aree.caipiao.tool.WebManager;

/**
 * 双色球中奖号码爬虫
 * @author hp-pc
 *
 */
public class ShsqClawer {
	private Log log = LogFactory.getLog(ShsqClawer.class);
	private IShuangSeQiuResultDao shuangSeQiuResultDao = new ShuangSeQiuResultDao();
	
	private WebManager manager = new WebManager();
	private String url = "http://kaijiang.zhcw.com/zhcw/html/ssq/list_{pageNo}.html";
	private String pageEncode = "utf-8";
	private int timepause = 0;
	
	public void claw() throws Exception {
		int pageNo = 1;
		do {
			String pageUrl = url.replace("{pageNo}", String.valueOf(pageNo));
			manager.setEncode(pageEncode);
			String page = manager.getSourceCode(pageUrl);
			if (StringUtils.isEmpty(page) || page.contains("404 Not Found")) {
				log.info("第" + pageNo + "页的内容不正确");
				break;
			}
			log.info("开始解析第" + pageNo + "页的彩票中奖信息");
			log.debug("页面内容为：" + page);
			String preHandledPage = preHandle(page);
			log.debug("预处理的内容：" + preHandledPage);
			String trReg = "<tr(.*?)>(.*?)</tr>";
			Pattern reg = Pattern.compile(trReg);
			Matcher match = reg.matcher(preHandledPage);
			while (match.find()) {
				String tr = match.group(2);
				if (!tr.contains("em")) continue;
				log.debug("tr：" + tr);
				String tdReg = "<td(.*?)>(.*?)</td>";
				Pattern tdPattern = Pattern.compile(tdReg);
				Matcher tdmatch = tdPattern.matcher(tr);
				
				StringBuffer buf = new StringBuffer();
				while (tdmatch.find()) {
					String td = tdmatch.group(2);
					log.debug("td：" + td);
					String dateReg = "^\\d{4}-\\d{2}-\\d{2}$";
					String serialReg = "^\\d{7}$";
					String numReg = "<em(.*?)>(.*?)</em>";
					String saleReg = "<strong>(.*?)</strong>";
					String top1NumReg = "<strong>(.*?)</strong>(.*?)";
					String top2NumReg = "<strongclass=\"rc\">(.*?)</strong>";
					if (td.matches(dateReg) || td.matches(serialReg)){
						buf.append(td).append(ShuangSeQiuResult.SPLITER);
					} else if (td.matches(numReg)) {
						log.debug("parse numbers");
						numReg = "<em(.*?)>(.*?)</em>";
						Pattern numPattern = Pattern.compile(numReg);
						Matcher numMatch = numPattern.matcher(td);
						while(numMatch.find()) {
							String number = numMatch.group(2);
							buf.append(number).append(ShuangSeQiuResult.SPLITER);
						}
					} else if (td.matches(saleReg)) {
						log.debug("parse sale");
						saleReg = "<strong>(.*?)</strong>";
						Pattern salePattern = Pattern.compile(saleReg);
						Matcher saleMatch = salePattern.matcher(td);
						if (saleMatch.find()) {
							String sale = saleMatch.group(1).replaceAll(",", "");
							buf.append(sale).append(ShuangSeQiuResult.SPLITER);
						}
					} else if (td.matches(top1NumReg)) {
						log.debug("parse top1Num");
						Pattern no1Pattern = Pattern.compile(top1NumReg);
						Matcher no1Match = no1Pattern.matcher(td);
						if (no1Match.find()) {
							String top1Num = no1Match.group(1);
							if (StringUtils.isNotEmpty(top1Num)) {
								buf.append(top1Num).append(ShuangSeQiuResult.SPLITER);
							}
						}
					} else if (td.matches(top2NumReg)) {
						log.debug("parse top2Num");
						Pattern no2Pattern = Pattern.compile(top2NumReg);
						Matcher no2Match = no2Pattern.matcher(td);
						if (no2Match.find()) {
							String top2Num = no2Match.group(1);
							if (StringUtils.isNotEmpty(top2Num)) {
								buf.append(top2Num).append(ShuangSeQiuResult.SPLITER);
							}
						}
					}
				}
				buf.deleteCharAt(buf.length() - 1);
				ShuangSeQiuResult result = ShuangSeQiuResult.build(buf.toString());
				log.info("Result:" + result);
				ShuangSeQiuResult dbResult = shuangSeQiuResultDao.getBySerial(result.getSerial());
				if (dbResult == null) {
					shuangSeQiuResultDao.saveShuangSeQiuResult(result);
				} else if (dbResult.getSale() == 0 && dbResult.getTop1Num() == 0 && dbResult.getTop2Num() == 0){
					result.setId(dbResult.getId());
					shuangSeQiuResultDao.updateShuangSeQiuResult(result);
				} else {
					log.info("exist record[" + result.getSerial() + "]");
					return;
				}
				/*Writer writer = new FileWriter("D:\\temp1.txt", true);
				writer.write(result.toString() + "\r\n");
				writer.close();*/
			}
			log.info("解析第" + pageNo + "页的彩票中奖信息结束");
			pageNo++;
			Thread.sleep(timepause);
 		} while (true);
	}
	
	private String preHandle(String page) {
		return page.replaceAll("\r", "").replaceAll("\n", "").replaceAll(" ", "").trim();
	}
	
	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getPageEncode() {
		return pageEncode;
	}

	public void setPageEncode(String pageEncode) {
		this.pageEncode = pageEncode;
	}

	public int getTimepause() {
		return timepause;
	}

	public void setTimepause(int timepause) {
		this.timepause = timepause;
	}

	public static void main(String[] args) {
		ShsqClawer clawer = new ShsqClawer();
		try {
			clawer.claw();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
